package xyz.bali16.module.invitecode.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.invitecode.entity.Invitation;
import xyz.bali16.module.invitecode.entity.InviteCode;
import xyz.bali16.module.invitecode.model.invitecode.InviteCodeAddParam;
import xyz.bali16.module.invitecode.model.invitecode.InviteCodePageParam;
import xyz.bali16.module.invitecode.model.invitecode.InviteCodeUpdateParam;
import xyz.bali16.module.invitecode.service.InviteCodeService;


import javax.validation.Valid;

import static xyz.bali16.application.core.model.Result.*;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"inviteCode")
@Api(tags = "邀请码接口")
@AllArgsConstructor
public class InviteCodeController extends BaseController{
    @Autowired
    InviteCodeService inviteCodeService;

    @GetMapping
    @ApiOperation("分页条件查询邀请码")
    //@PreAuthorize("@auth.permission('plugin:inviteCode:delete')")
    public Result<IPage<InviteCode>> pageQuery(@Valid InviteCodePageParam param) {
        IPage<InviteCode> inviteCode = inviteCodeService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, inviteCode);
    }

    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:inviteCode:add')")
    @ApiOperation("添加邀请码")
    public Result<String> add(@RequestBody @Valid InviteCodeAddParam param) {
        try {
            inviteCodeService.addInviteCode(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "新增邀请码失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@auth.permission('plugin:inviteCode:update')")
    @ApiOperation("修改邀请码")
    public Result<String> update(@RequestBody @Valid InviteCodeUpdateParam param) {
        try {
            inviteCodeService.updateInviteCode(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.UPDATE_ERROR, "更新邀请码失败", e);
        }
    }

    @DeleteMapping("/{inviteCodeId}")
    //@PreAuthorize("@auth.permission('plugin:inviteCode:delete')")
    @ApiOperation("删除邀请码")
    @ApiImplicitParam(name = "inviteCodeId", value = "邀请码id", paramType = "path", required = true)
    public Result<String> deleteInviteCode(@PathVariable("inviteCodeId") String inviteCodeId) {
        try {
            inviteCodeService.delete(inviteCodeId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
